package com.trassion.flutterhybirddemo

import android.content.Intent
import android.os.Bundle
import androidx.annotation.Nullable
import androidx.appcompat.app.AppCompatActivity
import com.trassion.flutterhybirddemo.databinding.ActivityFlutterFragmentBinding
import io.flutter.embedding.android.FlutterFragment


class FlutterFragmentActivity : AppCompatActivity() {
    lateinit var mViewBinding: ActivityFlutterFragmentBinding
    lateinit var flutterFragment :FlutterFragment
    lateinit var flutterFragment2 :FlutterFragment
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mViewBinding = ActivityFlutterFragmentBinding.inflate(layoutInflater)
        setContentView(mViewBinding.root)
         val fragmentManager = supportFragmentManager
         flutterFragment =
            if (fragmentManager.findFragmentByTag("flutterFragment") == null) FlutterFragment.withCachedEngine("myFlutterEngine3")
                .build() else fragmentManager.findFragmentByTag("flutterFragment") as FlutterFragment
         flutterFragment2 =
            if (fragmentManager.findFragmentByTag("flutterFragment2") == null) FlutterFragment.withNewEngine()
                .initialRoute("page_two")
                .build() else fragmentManager.findFragmentByTag("flutterFragment2") as FlutterFragment
        val transaction = fragmentManager.beginTransaction()

        transaction.add(mViewBinding.container1.id, flutterFragment!!, "flutterFragment")
        transaction.add(mViewBinding.container2.id, flutterFragment2!!, "flutterFragment2")
        transaction.commit()
    }

    override fun onPostResume() {
        super.onPostResume()
        flutterFragment.onPostResume()
        flutterFragment2.onPostResume()
    }

    override fun onNewIntent(intent: Intent) {
        super.onNewIntent(intent)
        flutterFragment.onNewIntent(intent)
        flutterFragment2.onNewIntent(intent)
    }

    override fun onBackPressed() {
        super.onBackPressed()
        flutterFragment.onBackPressed()
        flutterFragment2.onBackPressed()
    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<String?>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        flutterFragment.onRequestPermissionsResult(
            requestCode,
            permissions,
            grantResults
        )
        flutterFragment2.onRequestPermissionsResult(
            requestCode,
            permissions,
            grantResults
        )
    }

    override fun onActivityResult(
        requestCode: Int,
        resultCode: Int,
        data: Intent?
    ) {
        super.onActivityResult(requestCode, resultCode, data)
        flutterFragment.onActivityResult(
            requestCode,
            resultCode,
            data
        )
        flutterFragment2.onActivityResult(
            requestCode,
            resultCode,
            data
        )
    }

    override fun onUserLeaveHint() {
        flutterFragment.onUserLeaveHint()
        flutterFragment2.onUserLeaveHint()
    }

//    override fun onTrimMemory(level: Int) {
//        super.onTrimMemory(level)
//    }
//    override fun onTrimMemory(level: Int) {
//        super.onTrimMemory(level)
//        flutterFragment.onTrimMemory(level)
//        flutterFragment2.onTrimMemory(level)
//    }
}